﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KKU.RBSFPP.Shared.DAC;
using KKU.RBSFPP.Shared.Engines;

namespace KKU.RBSFPP.Web.Administrator.Controllers
{
    [Authorize(Users = "admin")]
    public class ProductUnitController : Controller
    {
        private RBSFPPModelContainer db = new RBSFPPModelContainer();

        //
        // GET: /ProductUnit/

        public ActionResult Index()
        {
            return View(db.ProductUnits.ToList().Display());
        }

        //
        // GET: /ProductUnit/Details/5

        public ActionResult Details(int id = 0)
        {
            ProductUnit productunit = db.ProductUnits.Single(p => p.Id == id);
            if (productunit == null)
            {
                return HttpNotFound();
            }
            return View(productunit);
        }

        //
        // GET: /ProductUnit/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /ProductUnit/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(ProductUnit productunit)
        {
            if (ModelState.IsValid)
            {
                productunit.Create();
                db.ProductUnits.AddObject(productunit);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(productunit);
        }

        //
        // GET: /ProductUnit/Edit/5

        public ActionResult Edit(int id = 0)
        {
            ProductUnit productunit = db.ProductUnits.Single(p => p.Id == id);
            if (productunit == null)
            {
                return HttpNotFound();
            }
            return View(productunit);
        }

        //
        // POST: /ProductUnit/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ProductUnit productunit)
        {
            if (ModelState.IsValid)
            {
                db.ProductUnits.Attach(productunit);
                db.ObjectStateManager.ChangeObjectState(productunit, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(productunit);
        }

        //
        // GET: /ProductUnit/Delete/5

        public ActionResult Delete(int id = 0)
        {
            ProductUnit productunit = db.ProductUnits.Single(p => p.Id == id);
            if (productunit == null)
            {
                return HttpNotFound();
            }
            return View(productunit);
        }

        //
        // POST: /ProductUnit/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ProductUnit productunit = db.ProductUnits.Single(p => p.Id == id);
            //db.ProductUnits.DeleteObject(productunit);
            productunit.Delete();
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}